SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[spAbmDivisionAux]

@Iddivision int,
@IDDivisionAux int = null,
@Mnemo varchar(50) = null,
@ABM int
 
AS

SET NOCOUNT ON
Declare @idsuborg  int
Declare @cod int
Declare @Diagnostico varchar(1000)



--Alta Individual 
if @ABM = 1 begin
Set @Diagnostico = '1'
select  @idsuborg =  idsuborg from tb_divisiones where iddivision = @iddivision

If Exists (Select * From tb_DivisionesAux Where  iddivision = @iddivision and iddivisionaux=@IDDivisionAux)
Begin
	Select @Cod = 2,@Diagnostico =  'Esta Division ya fue asignada al Grupo....'
	
	goto salir
End

BEGIN TRANSACTION

INSERT INTO tb_DivisionesAux (iddivision, iddivisionaux,mnemo,idsuborg)
VALUES (@Iddivision,@iddivisionaux,@mnemo,@idsuborg)


if @@Error = 0
	COMMIT TRANSACTION
else
	ROLLBACK TRANSACTION
end



--Delete Individual

if @ABM = 2 begin
BEGIN TRANSACTION
--Declare @idsuborg2  int
--select  @idsuborg2 =  idsuborg from tb_divisiones where iddivision = @iddivisionaux
DELETE FROM  tb_DivisionesAux  WHERE Iddivision= @Iddivision and iddivisionaux = @iddivisionaux --and idsuborg = @idsuborg2


if @@Error = 0
	COMMIT TRANSACTION
else
	ROLLBACK TRANSACTION
end

Salir:
select @cod as error, @Diagnostico as Diagnostico
GO
